package pers.zc.activiti.aop.testDemo;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @description
 * @Author Lin FuYuan
 * @date 2020/6/9
 */
@Async
@Aspect
@Component
public class AopDemo {
    public static ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>(4);
    /**
     * 定义切点
     * @author Lin FuYuan
     * @param
     * @date 2020/6/9
     * @return void
     */
    @Pointcut("execution(* pers.zc.activiti.aop.demo..*.*(..)) && @annotation(pers.zc.activiti.aop.testDemo.TestAnno)")
    public void pointCut(){
    }

    /**
     *  前置通知
     * @author Lin FuYuan
     * @param joinPoint
     * @date 2020/6/9
     * @return void
     */
    @Before(value="pointCut()")
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        TestAnno annotation = signature.getMethod().getAnnotation(TestAnno.class);
        String value = annotation.value();
        System.out.println("方法注解value:"+value);
        map.put(value,"null");
    }

    @AfterReturning(value="pointCut()",returning = "returnValue")
    public void saveSysLog(JoinPoint joinPoint,Object returnValue) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        TestAnno annotation = signature.getMethod().getAnnotation(TestAnno.class);
        String value = annotation.value();

        String after = "返回结果 :    "+JSON.toJSONString(returnValue);
        System.out.println(after);
        map.put(value,after);
    }
    @AfterThrowing(value="pointCut()",throwing = "e")
    public void throwing(JoinPoint joinPoint,Exception e){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        TestAnno annotation = signature.getMethod().getAnnotation(TestAnno.class);
        String value = annotation.value();

        String throwStr = e.toString();
        System.out.println("异常信息:"+throwStr);
        map.put(value,e.toString());
    }


}
